package com.meta.nd.factory;

import com.meta.common.ConstDef;
import com.meta.nd.vo.Fd;
import com.meta.nd.vo.Nd;
import com.meta.nd.vo.Nv;
import com.meta.nd.vo.Rd;
import org.springframework.util.StringUtils;

public class NdFactory {

public static Nd createNd(String metaClassName){
    if (StringUtils.isEmpty(metaClassName))
        return new Nd();
    Class clazz;
    try {
        clazz = Class.forName(metaClassName);
    } catch (ClassNotFoundException e) {
        String msg = String.format("Nd的类名不不存在:%s", metaClassName);
        throw new RuntimeException(msg,e);
    }
    try {
        Nd nd = (Nd)clazz.newInstance();
        return nd;
    } catch (InstantiationException e) {
        String msg = String.format("创建类实例失败，类名:%s", metaClassName);
        throw new RuntimeException(msg,e);
    } catch (IllegalAccessException e) {
        String msg = String.format("创建类实例失败，类名:%s", metaClassName);
        throw new RuntimeException(msg,e);
    }
}

public static Nd createNdByNv(Nv nv){
    String metaClassName = nv.getRawStringByName(ConstDef.FD_NAME_META_CLASS_NAME);
    Nd nd = NdFactory.createNd(metaClassName);
    nd.setNdCode(nv.getRawStringByName(ConstDef.FD_NAME_ND_CODE));
    nd.setNdName(nv.getRawStringByName(ConstDef.FD_NAME_ND_NAME));
    nd.setNdAlias(nv.getRawStringByName(ConstDef.FD_NAME_ND_ALIAS));
    nd.setNdDescription(nv.getRawStringByName(ConstDef.FD_NAME_ND_DESCRIPTION));
    nd.setMetaClassName(metaClassName);
    nd.setNvClassName(nv.getRawStringByName(ConstDef.FD_NAME_NV_CLASS_NAME));
    nd.setServiceClassName(nv.getRawStringByName(ConstDef.FD_NAME_SERVICE_CLASS_NAME));
    nd.setCategory(nv.getRawStringByName(ConstDef.FD_NAME_CATEGORY));
    nd.setDbMapCode(nv.getRawStringByName(ConstDef.FD_NAME_DB_MAP_CODE));
    nd.setUpdateTime(nv.getRawDateByName(ConstDef.FD_NAME_UPDATE_TIME));
    nd.setMinCount(nv.getRawIntByName(ConstDef.FD_NAME_MIN_COUNT));
    nd.setMaxCount(nv.getRawIntByName(ConstDef.FD_NAME_MAX_COUNT));
    nd.setCursive(nv.getRawBoolByName(ConstDef.FD_NAME_CURSIVE));
    for( Nv childNv : nv.getChildNvs(ConstDef.ND_NAME_ND)){
        Nd childNd = createNdByNv(childNv);
        nd.addChild(childNd);
    }
    for( Nv childNv : nv.getChildNvs(ConstDef.ND_NAME_FD)){
        Fd childFd = transferNvToFd(childNv);
        nd.getFds().add(childFd);
    }
    for (Nv childNv : nv.getChildNvs(ConstDef.ND_NAME_RD)){
        Rd childRd = transferNvToRd(childNv);
        nd.getRds().add(childRd);
    }
    return nd;
}

    private static Fd transferNvToFd(Nv nv){
        Fd fd = new Fd();
        fd.setFdCode(nv.getRawStringByName(ConstDef.FD_NAME_FD_CODE));
        fd.setFdName(nv.getRawStringByName(ConstDef.FD_NAME_FD_NAME));
        fd.setFdDescription(nv.getRawStringByName(ConstDef.FD_NAME_FD_DESCRIPTION));
        fd.setDataType(nv.getRawStringByName(ConstDef.FD_NAME_DATA_TYPE));
        fd.setSerNo(nv.getRawIntByName(ConstDef.FD_NAME_SER_NO));
        fd.setMdCode(nv.getRawStringByName(ConstDef.FD_NAME_MD_CODE));
        return fd;
    }

    private static Rd transferNvToRd(Nv nv){
        Rd rd = new Rd();
        rd.setOrderType(nv.getRawStringByName(ConstDef.FD_NAME_ORDER_TYPE));
        rd.setName(nv.getRawStringByName(ConstDef.FD_NAME_RD_NAME));
        rd.setCode(nv.getRawStringByName(ConstDef.FD_NAME_RD_CODE));
        rd.setTransactionType(nv.getRawStringByName(ConstDef.FD_NAME_TRANSACTION_TYPE));
        rd.setDescription(nv.getRawStringByName(ConstDef.FD_NAME_RD_DESCRIPTION));
        rd.setExecuteOrder(nv.getRawIntByName(ConstDef.FD_NAME_EXECUTE_ORDER));
        rd.setRuleText(nv.getRawStringByName(ConstDef.FD_NAME_RULE_TEXT));
        return rd;
    }

}
